﻿using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UntappdAPI.Tests
{
    [TestClass]
    public class APITests
    {
        private const string m_userName = "untappd_drunk";
        private const string m_password = "ap1_drunk";
        private const string m_checkinUserName = "untappd_drunk";
        private const string m_checkinPassword = "ap1_drunk";
        private const string m_apiKey = "API_KEY";

        private const string m_foursquareClientId = "CLIENT_ID";
        private const string m_foursquareSecret = "CLIENT_SECRET";

        private bool respComplete = false;

        //[TestMethod]
        //public void TestMethod1()
        //{
        //    var client = new UntappdClient(m_userName, m_password, m_apiKey);
        //    var parms = new Dictionary<string, string>();
        //    parms.Add("q", "fat+tire");
        //    var resp = client.TestMethod("beer_search", parms);

        //    Console.WriteLine(resp);
        //}

        [TestMethod]
        public void GetBeerInfoTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);

            client.FetchBeerInfo(6887);
            client.BeerInfoComplete += (sender, e) =>
                                           {
                                               Assert.IsTrue(e.Result.Results.Name.Equals("Fat Tire"));
                                               respComplete = true;
                                           };

            while (!respComplete)
            {

            }

            respComplete = false;
        }

        [TestMethod]
        public void GetBeerSearchResultsTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);

            client.FetchBeerSearchResults("fat+tire");
            client.BeerSearchResultsComplete += (sender, e) =>
                                                    {
                                                        Assert.IsTrue(e.Result.Results[0].Name.Equals("Fat Tire"));
                                                        respComplete = true;
                                                    };

            while (!respComplete)
            {

            }

            respComplete = false;
        }

        [TestMethod]
        public void GetBeerSearchResultsPagingTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);
            int totalRecords = 0;
            client.FetchBeerSearchResults("new belgium");
            client.BeerSearchResultsComplete += (sender, e) =>
            {
                if(e.Result.ReturnedResults == 25)
                {
                    totalRecords += e.Result.ReturnedResults;
                    client.FetchBeerSearchResults("new belgium", e.Result.NextOffset);
                }
                else
                {
                    Assert.IsTrue(totalRecords > 25, "Search returned less than 25 total records");
                    respComplete = true;
                }
            };

            while (!respComplete)
            {

            }

            respComplete = false;
        }

        [TestMethod]
        public void GetUserInfoTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);
            client.FetchUserInfo();
            client.UserInfoComplete += (sender, e) =>
            {
                Assert.IsTrue(e.Result.Results.User.UserName.Equals(client.UserName, StringComparison.OrdinalIgnoreCase));
                respComplete = true;
            };

            while (!respComplete)
            {

            }

            respComplete = false;
        }

        [TestMethod]
        public void GetUserFeedTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);
            client.FetchUserFeed();
            client.UserFeedComplete += (sender, e) =>
            {
                Assert.IsTrue(e.Result.Results[0].User.UserName.Equals(client.UserName, StringComparison.OrdinalIgnoreCase));
                var timeAgo = e.Result.Results[0].DisplayCreatedTimeAgo;
                Console.WriteLine(timeAgo);
                respComplete = true;
            };

            while (!respComplete) { }

            respComplete = false;
        }

        [TestMethod]
        public void GetFriendFeedTest()
        {
            var client = new UntappdClient(m_userName, m_password, m_apiKey);
            client.FetchFriendFeed();
            client.UserFeedComplete += (sender, e) =>
            {
                if (e.Result.Results.Count() > 0)
                {
                    Assert.IsTrue(e.Result.Results[0].BeerName.Length > 0);
                    var timeAgo = e.Result.Results[0].DisplayCreatedTimeAgo;
                    Console.WriteLine(timeAgo);
                    respComplete = true;
                }
                else
                {
                    respComplete = true;
                    Assert.Inconclusive("Either there are no results in this feed or there was an issue with this test.");
                }
                
            };

            while (!respComplete) { }

            respComplete = false;
        }

        [TestMethod]
        public void CheckinBasicTest()
        {
            var client = new UntappdClient(m_checkinUserName, m_checkinPassword, m_apiKey);
            client.CheckInBeer(6887, -6, null, null, null, null, false, false, false, true);
            client.CheckinComplete += (sender, e) =>
            {
                Assert.AreEqual(e.Result.CheckinResult, "success", true);
                respComplete = true;
            };

            while (!respComplete) { }

            respComplete = false;
        }

        [TestMethod]
        public void VenueSearchTest()
        {
            var client = new UntappdClient(m_checkinUserName, m_checkinPassword, m_apiKey, m_foursquareClientId, m_foursquareSecret);
            client.VenueSearch(41.878092f, -87.639553f);
            client.VenueSearchComplete += (sender, e) =>
            {
                if (e.Result.Count() == 0)
                    Assert.Inconclusive("No records returned from search. Verify test inputs");
                
                respComplete = true;
            };

            while (!respComplete) { }

            respComplete = false;
        }

    }
}
